package icraf.carbon.gui.chart;

import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FONT;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_LINE;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.common.gui.MenuAdapter;
import icraf.common.gui.MenuGenerator.IconModel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;

public class LineLabelSettingPanel extends JPanel{
	private static final long serialVersionUID = -4339756690912887507L;
	private FontChooser fontChooserA;
	private FontChooser fontChooserB;
	private ShapeSettingPanel lineSettPanelA;
	private ShapeSettingPanel lineSettPanelB;
	
	public LineLabelSettingPanel (MenuProperty menuA, MenuProperty menuB) {
		this.setLayout(new BorderLayout());
		fontChooserA = new FontChooser();
		fontChooserB = new FontChooser();
		lineSettPanelA = new ShapeSettingPanel(ShapeSettingPanel.LINE_SETTING);
		lineSettPanelB = new ShapeSettingPanel(ShapeSettingPanel.LINE_SETTING);

		JPanel tabAxis = new JPanel(new BorderLayout());
		JPanel toolbar = new JPanel();
		tabAxis.add(toolbar, BorderLayout.NORTH);
		
		JPanel chardPanelAxis = new JPanel(new CardLayout());
		chardPanelAxis.add(fontChooserA, CHART_FONT.toString());
		chardPanelAxis.add(lineSettPanelA, CHART_LINE.toString());
		tabAxis.add(chardPanelAxis);
		
		ButtonGroup group = new ButtonGroup();
		addButton(CHART_FONT, chardPanelAxis, toolbar, group);
		addButton(CHART_LINE, chardPanelAxis, toolbar, group);
		
		
		JPanel tabUnit = new JPanel(new BorderLayout());
		JPanel toolbarUnit = new JPanel();
		tabUnit.add(toolbarUnit, BorderLayout.NORTH);
		
		JPanel chardPanelUnit = new JPanel(new CardLayout());
		chardPanelUnit.add(fontChooserB, CHART_FONT.toString());
		chardPanelUnit.add(lineSettPanelB, CHART_LINE.toString());
		tabUnit.add(chardPanelUnit);
		
		ButtonGroup groupUnit = new ButtonGroup();
		addButton(CHART_FONT, chardPanelUnit, toolbarUnit, groupUnit);
		addButton(CHART_LINE, chardPanelUnit, toolbarUnit, groupUnit);
		
		JTabbedPane tabPane = new JTabbedPane();
		tabPane.addTab(menuA.getLabel(), menuA.getIcon(IconModel.ICON16), tabAxis);
		tabPane.addTab(menuB.getLabel(), menuB.getIcon(IconModel.ICON16), tabUnit);

		this.add(tabPane);
	}
	
	private void addButton(final MenuAdapter m,
			final JPanel cards, JPanel bar, ButtonGroup group) {
		Action action = new AbstractAction(m.getLabel(), m.getIcon(IconModel.ICON16)) {
			private static final long serialVersionUID = 5073139705808499427L;
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout)(cards.getLayout());
			    cl.show(cards, m.toString());
			}
		};

		JToggleButton button = new JToggleButton(action);
		bar.add(button);
		
		group.add(button);
		if (group.getSelection() == null) {
			button.setSelected(true);
			CardLayout cl = (CardLayout)(cards.getLayout());
		    cl.show(cards, m.toString());
		}
	}
	

	
	public void setSettingA(LineLabelFormat s) {
		fontChooserA.setSettingFont(s.font);
		fontChooserA.setSettingColor(s.fontColor);
		lineSettPanelA.setSetting(s);
	}
	
	public void setSettingB(LineLabelFormat s) {
		fontChooserB.setSettingFont(s.font);
		fontChooserB.setSettingColor(s.fontColor);
		lineSettPanelB.setSetting(s);
	}
	
	public LineLabelFormat getSettingA(LineLabelFormat s) {
		s = (LineLabelFormat) lineSettPanelA.getSetting(s);
		s.font = fontChooserA.getSettingFont();
		s.fontColor = fontChooserA.getSettingColor();
		return s;
	}
	
	public LineLabelFormat getSettingB(LineLabelFormat s) {
		s = (LineLabelFormat) lineSettPanelB.getSetting(s);
		s.font =fontChooserB.getSettingFont();
		s.fontColor = fontChooserB.getSettingColor();
		return s;
	}
}